﻿<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
      <a-radio-group
        v-model="queryParam[0].keyword"
        style="margin-left: 8px"
        :default-value="null"
        button-style="solid"
        @change="getDataList()"
      >
        <a-radio-button :value="null">全部</a-radio-button>
        <a-radio-button v-for="item in this.GiftTypes" :key="item.DicKey" :value="item.DicName"> {{ item.DicName }}</a-radio-button>
      </a-radio-group>
    </div>
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam[1].keyword" placeholder="客户名称" @keydown.enter="getDataList"/>
            </a-form-item>
          </a-col>
          <a-col :md="2" :sm="24">
            <a-form-item>
              <a-month-picker :value="time" @change="onAllocateTimeChange" :allowClear="false"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
            >查询
            </a-button
            >
            <a-button style="margin-left: 8px" @click="Repossess">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-table
      ref="table"
      :scroll="{ y: tableHeight }"
      :columns="columns"
      :rowKey="(row, index) => index"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :bordered="true"
      size="small"
    >
      <span slot="SaleiQuantity" slot-scope="SaleiQuantity" v-format="'#,##0'">{{ SaleiQuantity }}</span>
      <span slot="SaleiSum" slot-scope="SaleiSum" v-format="'#,##0.00'">{{ SaleiSum }}</span>
      <template slot="ShouldQuantity" slot-scope="text, record">
        <span v-if="record.GiftType == '销售返料'" v-format="'#,##0'">{{ text }}</span>
        <span v-if="record.GiftType == '销售返利'" v-format="'#,##0.00'">{{ text }}</span>
        <span v-else v-format="'#,##0'">{{ 0 }}</span>
      </template>
      <template slot="rdsiQuantity" slot-scope="text, record">
        <span v-if="record.GiftType == '销售返料'" v-format="'#,##0'">{{ Number(text) }}</span>
        <span v-if="record.GiftType == '销售返利'" v-format="'#,##0.00'">{{ Number(text) }}</span>
        <span v-else v-format="'#,##0'">{{ Number(0) }}</span>
      </template>
      <template slot="NotiQuantity" slot-scope="text, record">
        <span v-if="record.GiftType == '销售返料'" v-format="'#,##0'">{{ text }}</span>
        <span v-if="record.GiftType == '销售返利'" v-format="'#,##0.00'">{{ text }}</span>
        <span v-else v-format="'#,##0'">{{ 0 }}</span>
      </template>
    </a-table>
  </a-card>
</template>

<script>
import moment from 'moment'

const columns = [
  { title: '客户编码', dataIndex: 'cCusCode', align: 'center', width: 100 },
  { title: '客户名称', dataIndex: 'cCusName', align: 'left', width: 220, ellipsis: true },
  { title: '返料规则', dataIndex: 'RuleName', align: 'left', width: 180 },
  {
    title: '销售出库',
    children: [
      {
        title: '品名',
        dataIndex: 'SalePId',
        align: 'left',
        width: '20%',
        customRender: (text, record) => {
          if (text) {
            return record.SalePId + '/' + record.SaleName
          } else {
            return '无返料信息'
          }
        }
      },
      {
        title: '订单数量',
        dataIndex: 'SaleiQuantity',
        align: 'right',
        width: '6%',
        scopedSlots: { customRender: 'SaleiQuantity' }
      },
      {
        title: '订单金额(元)',
        dataIndex: 'SaleiSum',
        align: 'right',
        width: '8%',
        scopedSlots: { customRender: 'SaleiSum' }
      }
    ]
  },
  {
    title: '返料/返利',
    children: [
      {
        title: '品名',
        dataIndex: 'GiftPId',
        align: 'left',
        width: '20%',
        customRender: (text, record) => {
          if (text) {
            return record.GiftPId + '/' + record.GiftName
          }
        }
      },
      {
        title: '应返',
        dataIndex: 'ShouldQuantity',
        align: 'right',
        width: '8%',
        scopedSlots: { customRender: 'ShouldQuantity' }
      },
      {
        title: '已返',
        dataIndex: 'rdsiQuantity',
        align: 'right',
        width: '8%',
        scopedSlots: { customRender: 'rdsiQuantity' }
      },
      {
        title: '欠返',
        dataIndex: 'NotiQuantity',
        align: 'right',
        width: '8%',
        scopedSlots: { customRender: 'NotiQuantity' }
      }
    ]
  }
]

export default {
  mounted () {
    this.$nextTick(() => {
      // 页面渲染完成后的回调
      this.tableHeight = this.getTableHeight(true) + 24
    })
    this.time = moment(new Date(), 'YYYY-MM')
    this.queryParam[2].keyword = moment().format('YYYY-MM')
    this.getGiftTypes()
    this.getDataList()
  },
  data () {
    return {
      tableHeight: 570, // 表格高度
      data: [],
      columns,
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      GiftTypes: [],
      time: null,
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      queryParam: [{ condition: 'GiftType' }, { condition: 'cCusName' }, { condition: 'DateOfMonth' }]
    }
  },
  methods: {
    Repossess () {
      this.queryParam = [{ condition: 'GiftType', keyword: null }, { condition: 'cCusName' }, { condition: 'DateOfMonth', keyword: moment().format('YYYY-MM') }]
      this.time = moment().format('YYYY-MM')
    },
    onAllocateTimeChange (dates, dateStrings) {
      this.queryParam[2].keyword = dateStrings
      this.time = dates
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.getDataList()
    },
    getGiftTypes () {
      this.$http
        .post('/Base_BasicData/Base_DictionaryMain/GetTheDataByDicKey', {
          Condition: 'DicKey',
          Keyword: 'MaterialReturnType'
        })
        .then(res => {
          this.GiftTypes = res.Data.DictionaryDetailsList
        })
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/GiftManagement/GMM_GiftMaterialApplyDetailsTotal/GetDataListOfGiftMaterialAccrual', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    valueFormat (val) {
      return this.textFormat(val)
    },
    timeFormat (val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD HH:mm')
    }
  }
}
</script>
